//
//  YJDViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/1.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "YJDViewController.h"
#import "HJCarDeailViewController.h"
#import "YJDCell.h"
@interface YJDViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UIImageView *imageView;
    UILabel *labTit;
}

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic, strong) NSArray *urlArray;

@property (nonatomic, strong) NSMutableArray *orderArray;

@end

@implementation YJDViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"已接单"];
    self.urlArray = [[NSArray alloc]initWithObjects:@"",@"",@"/App/Ordermanage/wcbuycaroder",@"/App/Ordermanage/wcorderwashcar",@"/App/Ordermanage/wchousingorder",@"/App/Ordermanage/wcontheorder",@"/App/Ordermanage/yjcoolieorder",@"/App/Ordermanage/yjcleaningorder",@"/App/Ordermanage/yjhousekeepingorder",@"/App/Ordermanage/wcunlockingorder",@"/App/Ordermanage/yjvegetablesorder", nil];
    
    [self loadUI];
    [self.view setBackgroundColor:HexColorInt32_t(F2F2F2)];
    
    [self loadData];
}



- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    [self.tableView setBackgroundColor:HexColorInt32_t(F2F2F2)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"available"]];
    imageView.hidden = YES;
    [self.view addSubview:imageView];
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
    
    labTit = [[UILabel alloc] init];
    labTit.text = @"暂无数据...";
    labTit.hidden = YES;
    labTit.font = [UIFont systemFontOfSize:15.0];
    labTit.textColor = [UIColor lightGrayColor];
    [self.view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(imageView.bottom).offset(8);
    }];
    
}

- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:_businessId forKey:@"merchantid"];
    [parmas setValue:@"1" forKey:@"p"];
    NSInteger urlIndex = [self.merType integerValue];
    NSString *urlString = [self.urlArray objectAtIndex:urlIndex];
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/wcbuycaroder",urlHead];

    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *error = [responseObject objectForKey:@"error"];
        NSString *mssage = [responseObject objectForKey:@"mssage"];
        NSMutableArray *result = [responseObject objectForKey:@"result"];
        if ([error integerValue] == 200 && [result isKindOfClass:[NSArray class]]) {
            self.orderArray = [result mutableCopy];
            [self.tableView reloadData];
            
            _tableView.hidden = NO;
            imageView.hidden = YES;
            labTit.hidden = YES;
        }else{
            _tableView.hidden = YES;
            imageView.hidden = NO;
            labTit.hidden = NO;
            
        }
        [LICGToast showWithText:mssage];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    YJDCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YJDCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.btnjd.hidden = YES;
    NSDictionary *itemDic = [self.orderArray objectAtIndex:indexPath.row];
    [cell.labDDH setText:[NSString stringWithFormat:@"订单号：%@",[itemDic objectForKey:@"ordernumber"]]];
    [cell.labSth setText:[itemDic objectForKey:@"ordername"]];
    [cell.labDate setText:[self getNewDate:[itemDic objectForKey:@"addtime"]]];
    [cell.labPrice setText:[itemDic objectForKey:@"ordermoney"]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *itemDic = [self.orderArray objectAtIndex:indexPath.row];
    HJCarDeailViewController *vc = [[HJCarDeailViewController alloc]init];
    vc.meri_id = self.businessId;
    vc.merchantid = itemDic[@"id"];
    vc.type = 2;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSMutableArray *)orderArray{
    if (!_orderArray) {
        _orderArray = [[NSMutableArray alloc]init];
    }
    return _orderArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
